package com.js.data.dtmg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.js.data.common.utils.ListUtil;
import com.js.data.dtmg.dto.DataQuery;
import com.js.data.dtmg.dto.DtTaskLogQuery;
import com.js.data.dtmg.entity.DtTaskLog;
import com.js.data.dtmg.entity.DtTask;
import com.js.data.dtmg.mapper.DtTaskLogMapper;
import com.js.data.dtmg.mapper.SqlMapper;
import com.js.data.dtmg.service.DtTaskLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.js.data.dtmg.service.DtTaskService;
import com.js.data.dtmg.vo.DtCrontabLogVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 定时任务日志表 服务实现类
 * </p>
 *
 * @author hh
 * @since 2024-07-19
 */
@Service
public class DtTaskLogServiceImpl extends ServiceImpl<DtTaskLogMapper, DtTaskLog> implements DtTaskLogService {
    @Resource
    SqlMapper sqlMapper;
    @Resource
    DtTaskService taskService;
    /**
     * 分页
     *
     * @param query 查询条件
     * @return IPage<UserPageVO>
     */
    @Override
    public IPage<DtCrontabLogVO> pageList(DtTaskLogQuery query){
        Page<DtTaskLog> inPage = new Page<>(query.getCurrent(), query.getSize());
        LambdaQueryWrapper<DtTaskLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(query.getTaskName()), DtTaskLog::getTaskName,query.getTaskName());

        queryWrapper.orderByDesc(DtTaskLog::getUpdateTime);
        page(inPage,queryWrapper);
        IPage<DtCrontabLogVO> retPage = new Page<>();
        BeanUtils.copyProperties(inPage, retPage);
        List<DtTaskLog> records =  inPage.getRecords();
        retPage.setRecords(ListUtil.copyListToList(records,DtCrontabLogVO.class));
        return retPage;
    }

    @Override
    public  IPage<Map<String,Object>> dataPageList(DataQuery query){
        Page<Map<String, Object>> pageInfo = new Page<>(query.getCurrent(), query.getSize());
        DtTaskLog crontabLog = getById(query.getLogId());
        DtTask task = taskService.getById(crontabLog.getTaskId());
        String sql = "select * from "+task.getOdsTableName()+" where in_time_no='"+crontabLog.getInTimeNo()+"'";
        return sqlMapper.getSqlData(pageInfo,sql);
    }

    @Override
    public void redoTask(Long logId){

        //记录任务日志
        DtTaskLog dtTaskLog = getById(logId);
        //0处理中
        dtTaskLog.setResult(0);
        dtTaskLog.setBegTime(LocalDateTime.now());
        updateById(dtTaskLog);
        //处理任务信息
        DtTask task = taskService.getById(dtTaskLog.getTaskId());
        task.setInParam(dtTaskLog.getInParam());

        Map<String,Object> mapRtn = taskService.doTask(task, dtTaskLog.getInTimeNo());
        String errMsg = (String)mapRtn.get("errMsg");
        if (com.js.data.common.utils.StringUtils.isEmpty(errMsg)) {
            //1处理成功 2处理失败
            dtTaskLog.setResult(1);
            dtTaskLog.setInNum(Long.parseLong(String.valueOf(mapRtn.get("inNum"))));
            dtTaskLog.setDoNum(Long.parseLong(String.valueOf(mapRtn.get("doNum"))));
        } else {
            dtTaskLog.setResult(2);
            dtTaskLog.setResultRemarks(errMsg);
        }
        dtTaskLog.setInTimeNo((String)mapRtn.get("inTimeNo"));
        dtTaskLog.setEndTime(LocalDateTime.now());
        updateById(dtTaskLog);
    }

}
